package com.github.leecho.ssc.captcha.repository.impl;

import com.github.leecho.ssc.captcha.repository.CaptchaRepository;

import javax.servlet.http.HttpServletRequest;

/**
 * @author LIQIU
 * created on 2020/11/13
 **/
public class SessionCaptchaRepository implements CaptchaRepository<HttpServletRequest> {

    private static final String CAPTCHA_SESSION_KEY = "captcha";

    /**
     * the key of captcha in session attributes
     */
    private String captchaSessionKey = CAPTCHA_SESSION_KEY;

    @Override
    public String load(HttpServletRequest request) {
        return (String) request.getSession().getAttribute(captchaSessionKey);
    }

    @Override
    public void save(HttpServletRequest request, String captcha) {
        request.getSession().setAttribute(captchaSessionKey, captcha);
    }

    /**
     * @return sessionKey
     */
    public String getCaptchaSessionKey() {
        return captchaSessionKey;
    }

    /**
     * @param captchaSessionKey sessionKey
     */
    public void setCaptchaSessionKey(String captchaSessionKey) {
        this.captchaSessionKey = captchaSessionKey;
    }
}
